"""
环信接口封装
"""
import json

from .hxConfigParser import HxConfigParser
from .ApiBase import ApiBase


class HxApi(ApiBase):
    def __init__(self):
        self.conf = HxConfigParser()

    def update_app_token(self):
        """
        更新管理员token
        """
        base_url = self.conf.get_url()
        url = f"{base_url}/token"
        headers = self.get_headers()
        method = "POST"
        payload = {
            "grant_type": "client_credentials",
            "client_id": self.conf.get_client_id(),
            "client_secret": self.conf.get_client_secret()
        }
        result = self.request(url=url, headers=headers, method=method, payload=payload)
        self.conf.update_app_token(result.get("access_token"))
        return "ok"

    def register_user(self, **kwargs):
        """
        注册用户
        """
        # 取参
        # --- start ---
        username = kwargs.get("username")
        password = kwargs.get("password")
        # --- end ---
        url = f"{self.conf.get_url()}/users"
        headers = self.get_token_headers()
        method = "POST"
        payload = {
            "username": username,
            "password": password
        }
        return self.request(url=url, headers=headers, method=method, payload=payload)

    def get_token_headers(self):
        """
        获取带有token的headers
        """
        headers = self.get_headers()
        headers["Authorization"] = f"Bearer {self.conf.get_app_token()}"
        return headers

    def contact_user(self, **kwargs):
        """
        添加好友
        """
        # 取参
        # --- start ---
        owner_username = kwargs.get("owner_username")
        friend_username = kwargs.get("friend_username")
        # --- end ---
        url = f"{self.conf.get_url()}/users/{owner_username}/contacts/users/{friend_username}"
        headers = self.get_token_headers()
        method = "POST"
        payload = {}
        return self.request(url=url, headers=headers, method=method, payload=payload)

    def send_message(self, **kwargs):
        """
        发送消息
        """
        # 取参
        # --- start ---
        message_type = kwargs.get("message_type")
        from_user = kwargs.get("from_user")
        to_user = kwargs.get("to_user")
        message = kwargs.get("message")
        # --- end ---
        url = f"{self.conf.get_url()}/messages/users"
        headers = self.get_token_headers()
        method = "POST"
        payload = {
            "from": from_user,
            "to": to_user,
            "type": message_type,
            "body": {
                "msg": message
            }
        }
        return self.request(url=url, headers=headers, method=method, payload=payload)

    def make_chatgroup(self, *args, **kwargs):
        """
        创建群组
        """
        # 取参
        # --- start ---
        groupname = kwargs.get("groupname")
        description = kwargs.get("description")
        owner = kwargs.get("owner")
        custom = kwargs.get("custom")
        # --- end ---
        url = f"{self.conf.get_url()}/chatgroups"
        headers = self.get_token_headers()
        method = "POST"
        payload = {
            "groupname": groupname,
            "description": description,
            "public": True,
            "owner": owner,
            "members": args,
            "custom": json.dumps(custom)
        }
        return self.request(url=url, headers=headers, method=method, payload=payload)

    def send_message_to_chatgroup(self, **kwargs):
        """
        向群组发送消息
        """
        # --- start ---
        message_type = kwargs.get("message_type")
        from_user = kwargs.get("from_user")
        to_groupid = kwargs.get("to_groupid")
        message = kwargs.get("message")
        ext = kwargs.get("ext")
        # --- end ---
        url = f"{self.conf.get_url()}/messages/chatgroups"
        headers = self.get_token_headers()
        method = "POST"
        payload = {
            "from": from_user,
            "to": to_groupid,
            "type": message_type,
            "ext": ext,
            "body": {
                "msg": message
            }
        }
        return self.request(url=url, headers=headers, method=method, payload=payload)

    def join_chatgroup(self, **kwargs):
        """
        加入聊天群组
        """
        group_id = kwargs.get("group_id")
        username = kwargs.get("username")
        baseUrl = self.conf.get_url()
        url = f"{baseUrl}/chatgroups/{group_id}/users/{username}"
        headers = self.get_token_headers()
        method = "POST"
        payload = {

        }
        return self.request(url=url, headers=headers, method=method, payload=payload)
